/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dialogs.perfil;

import dao.Dao;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ListModel;
import libcontrol.model.Perfil;
import libcontrol.model.Atividade;

/**
 *
 * @author filipe
 */
public class JDialogCadastrarAlterarPerfil extends javax.swing.JDialog {

    private Perfil perfil;

    /**
     * Creates new form JDialogCadastrarPerfil
     */
    public JDialogCadastrarAlterarPerfil(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        DefaultListModel<Atividade> model = new DefaultListModel<>();
        for (Atividade a : Atividade.values()) {
            model.addElement(a);
        }
        jListAtividadesPadrao.setModel(model);
        model = new DefaultListModel<>();
        jListAtividadesAdicionadas.setModel(model);
        perfil = new Perfil();
    }

    JDialogCadastrarAlterarPerfil(JFrame parent, boolean modal, Perfil perfil) {
        super(parent, modal);
        initComponents();
        this.perfil = perfil;
        preencherCampos();
        if (perfil.getNome().equals("Administrador")) {
            jTextFieldNome.setEnabled(false);
            jListAtividadesAdicionadas.setEnabled(false);
            jListAtividadesPadrao.setEnabled(false);
            jButtonAdicionar.setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelPerfil = new javax.swing.JPanel();
        jLabelNome = new javax.swing.JLabel();
        jTextFieldNome = new javax.swing.JTextField();
        jLabelTempoMaximo = new javax.swing.JLabel();
        jLabelNumeroMaximo = new javax.swing.JLabel();
        jTextFieldTempoMaximo = new javax.swing.JTextField();
        jTextFieldNumeroMaximo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListAtividadesAdicionadas = new javax.swing.JList();
        jButtonAdicionar = new javax.swing.JButton();
        jButtonRemover = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListAtividadesPadrao = new javax.swing.JList();
        jLabelAtividades = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jLabelDias = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelNome.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelNome.setText("Nome do Perfil:");

        jLabelTempoMaximo.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelTempoMaximo.setText("Tempo Máximo de Empréstimo:");

        jLabelNumeroMaximo.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelNumeroMaximo.setText("Número Máximo de Empréstimos:");

        jListAtividadesAdicionadas.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListAtividadesAdicionadas.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListAtividadesAdicionadasValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListAtividadesAdicionadas);

        jButtonAdicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/adicionar_menor.png"))); // NOI18N
        jButtonAdicionar.setText("Adicionar");
        jButtonAdicionar.setEnabled(false);
        jButtonAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAdicionarActionPerformed(evt);
            }
        });

        jButtonRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cancelar_menor.png"))); // NOI18N
        jButtonRemover.setText("Remover");
        jButtonRemover.setEnabled(false);
        jButtonRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoverActionPerformed(evt);
            }
        });

        jListAtividadesPadrao.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListAtividadesPadrao.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListAtividadesPadraoValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jListAtividadesPadrao);

        jLabelAtividades.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelAtividades.setText("Atividades Permitidas para esse Perfil");

        jButtonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/perfil.png"))); // NOI18N
        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/cancelar.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabelDias.setFont(new java.awt.Font("Arial Black", 0, 14)); // NOI18N
        jLabelDias.setText("dias");

        javax.swing.GroupLayout jPanelPerfilLayout = new javax.swing.GroupLayout(jPanelPerfil);
        jPanelPerfil.setLayout(jPanelPerfilLayout);
        jPanelPerfilLayout.setHorizontalGroup(
            jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPerfilLayout.createSequentialGroup()
                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelPerfilLayout.createSequentialGroup()
                        .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelPerfilLayout.createSequentialGroup()
                                .addGap(24, 24, 24)
                                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelTempoMaximo)
                                    .addComponent(jLabelNome))
                                .addGap(28, 28, 28))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPerfilLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabelNumeroMaximo)
                                    .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelPerfilLayout.createSequentialGroup()
                                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jTextFieldNumeroMaximo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                                    .addComponent(jTextFieldTempoMaximo, javax.swing.GroupLayout.Alignment.LEADING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabelDias))
                            .addGroup(jPanelPerfilLayout.createSequentialGroup()
                                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jButtonAdicionar)
                                    .addComponent(jButtonRemover))
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButtonSalvar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelPerfilLayout.createSequentialGroup()
                        .addGap(195, 195, 195)
                        .addComponent(jLabelAtividades)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelPerfilLayout.setVerticalGroup(
            jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPerfilLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNome)
                    .addComponent(jTextFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelTempoMaximo)
                    .addComponent(jTextFieldTempoMaximo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDias))
                .addGap(18, 18, 18)
                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNumeroMaximo)
                    .addComponent(jTextFieldNumeroMaximo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addComponent(jLabelAtividades)
                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelPerfilLayout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jButtonAdicionar)
                        .addGap(149, 149, 149)
                        .addComponent(jButtonRemover))
                    .addGroup(jPanelPerfilLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                            .addComponent(jScrollPane2))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                .addGroup(jPanelPerfilLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 8, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPerfil, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        perfil.setNome(jTextFieldNome.getText());
        perfil.setMaxEmprestimos(Integer.parseInt(jTextFieldNumeroMaximo.getText()));
        perfil.setTempoEmprestimo(Integer.parseInt(jTextFieldTempoMaximo.getText()));                
        ListModel model = jListAtividadesAdicionadas.getModel();
        ArrayList<Atividade> atividades = new ArrayList<>();
        for (int i = 0; i < model.getSize(); i++) {
            atividades.add((Atividade) model.getElementAt(i));
        }
        perfil.setAtividades(atividades);
        if (perfil.getId() == null) {
            new Dao(Perfil.class).insert(perfil);
            JOptionPane.showMessageDialog(this, "Cadastrado com sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
            setVisible(false);
            new JDialogVerPerfil((JFrame) getParent(), true, perfil).setVisible(true);
            dispose();
        } else {
            new Dao(Perfil.class).update(perfil);
            JOptionPane.showMessageDialog(this, "Alterado com sucesso!", "Sucesso", JOptionPane.INFORMATION_MESSAGE);
            setVisible(false);
            new JDialogVerPerfil((JFrame) getParent(), true, perfil).setVisible(true);
            dispose();
        }
    }//GEN-LAST:event_jButtonSalvarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAdicionarActionPerformed
        DefaultListModel modelPadrao = (DefaultListModel) jListAtividadesPadrao.getModel();
        DefaultListModel modelAdicionadas = (DefaultListModel) jListAtividadesAdicionadas.getModel();
        modelAdicionadas.addElement(modelPadrao.remove(jListAtividadesPadrao.getSelectedIndex()));
        jListAtividadesPadrao.setModel(modelPadrao);
        jListAtividadesAdicionadas.setModel(modelAdicionadas);
    }//GEN-LAST:event_jButtonAdicionarActionPerformed

    private void jButtonRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoverActionPerformed
        DefaultListModel modelPadrao = (DefaultListModel) jListAtividadesPadrao.getModel();
        DefaultListModel modelAdicionadas = (DefaultListModel) jListAtividadesAdicionadas.getModel();
        modelPadrao.addElement(modelAdicionadas.remove(jListAtividadesAdicionadas.getSelectedIndex()));
        jListAtividadesPadrao.setModel(modelPadrao);
        jListAtividadesAdicionadas.setModel(modelAdicionadas);
    }//GEN-LAST:event_jButtonRemoverActionPerformed

    private void jListAtividadesPadraoValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListAtividadesPadraoValueChanged
        jButtonRemover.setEnabled(false);
        jButtonAdicionar.setEnabled(true);
    }//GEN-LAST:event_jListAtividadesPadraoValueChanged

    private void jListAtividadesAdicionadasValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListAtividadesAdicionadasValueChanged
        jButtonAdicionar.setEnabled(false);
        jButtonRemover.setEnabled(true);
    }//GEN-LAST:event_jListAtividadesAdicionadasValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdicionar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonRemover;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JLabel jLabelAtividades;
    private javax.swing.JLabel jLabelDias;
    private javax.swing.JLabel jLabelNome;
    private javax.swing.JLabel jLabelNumeroMaximo;
    private javax.swing.JLabel jLabelTempoMaximo;
    private javax.swing.JList jListAtividadesAdicionadas;
    private javax.swing.JList jListAtividadesPadrao;
    private javax.swing.JPanel jPanelPerfil;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextFieldNome;
    private javax.swing.JTextField jTextFieldNumeroMaximo;
    private javax.swing.JTextField jTextFieldTempoMaximo;
    // End of variables declaration//GEN-END:variables

    private void preencherCampos() {
        jTextFieldNome.setText(perfil.getNome());
        jTextFieldNumeroMaximo.setText(String.valueOf(perfil.getMaxEmprestimos()));
        jTextFieldTempoMaximo.setText(String.valueOf(perfil.getTempoEmprestimo()));
        DefaultListModel<Atividade> modelPadrao = new DefaultListModel<>();
        DefaultListModel<Atividade> modelAdicionadas = new DefaultListModel<>();
        for (Atividade a : Atividade.values()) {
            if (perfil.getAtividades().contains(a))
                modelAdicionadas.addElement(a);
            else {
            modelPadrao.addElement(a);
            }
        }
        jListAtividadesPadrao.setModel(modelPadrao);        
        jListAtividadesAdicionadas.setModel(modelAdicionadas);
    }
}
